# Estimate the conditional effects of a continuous measure of populist parties.
# See Figure N.5, Appendix N.




#############
# load data #
#############

library(brms)
library(ggplot2)
library(ggpubr)
library(stargazer)

load("PSRM Replication Files/PresidentialElectionsPosts.RData")

colnames(president_df)

all(c("populist_continuous", "polarization") %in% colnames(president_df)) # TRUE

length(unique(president_df$country)) # 26
length(unique(president_df$electionname)) # 29
length(unique(president_df$party_country)) # 52

# focus on 15 days before and after the election
day_range <- 15

sub <- president_df[which(president_df$daysinceelection >= -1*day_range
                          & president_df$daysinceelection <= day_range),]

# winners and losers
winners <- sub[which(sub$win == 1),]
losers <- sub[which(sub$win != 1),]

length(unique(winners$electionname[which(!is.na(winners$polarization))])) # 27
length(unique(winners$electionname[which(!is.na(winners$economic_polarization))])) # 27
length(unique(winners$electionname[which(!is.na(winners$social_polarization))])) # 27
length(unique(winners$electionname[which(!is.na(winners$populist_present))])) # 27
length(unique(winners$electionname[which(!is.na(winners$populist_dummy))])) # 27

length(unique(losers$electionname[which(!is.na(losers$polarization))])) # 27
length(unique(losers$electionname[which(!is.na(losers$economic_polarization))])) # 27
length(unique(losers$electionname[which(!is.na(losers$social_polarization))])) # 27
length(unique(losers$electionname[which(!is.na(losers$populist_present))])) # 27
length(unique(losers$electionname[which(!is.na(losers$populist_dummy))])) # 27




##################
# winners + love #
##################

winner_love_pp <- brm(loveprop ~ post*populist_continuous + post*polarization
                      + incumbentparty + concurrent + runoff + semipresidential + enc_v1
                      + Xl + Xr 
                      + (1 + Xl + Xr | party_election),
                      data=winners,
                      warmup=1000, iter=2000, chains=3, seed=1234)
#plot(winner_love_pp)
#summary(winner_love_pp)




###################
# winners + angry #
###################

winner_angry_pp <- brm(angryprop ~ post*populist_continuous + post*polarization
                       + incumbentparty + concurrent + runoff + semipresidential + enc_v1
                       + Xl + Xr 
                       + (1 + Xl + Xr | party_election),
                       data=winners,
                       warmup=1000, iter=2000, chains=3, seed=1234)
#plot(winner_angry_pp)
#summary(winner_angry_pp)




#################
# losers + love #
#################

loser_love_pp <- brm(loveprop ~ post*populist_continuous + post*polarization
                     + incumbentparty + concurrent + runoff + semipresidential + enc_v1
                     + Xl + Xr 
                     + (1 + Xl + Xr | party_election),
                     data=losers,
                     warmup=1000, iter=2000, chains=3, seed=1234)
#plot(loser_love_pp)
#summary(loser_love_pp)




##################
# losers + angry #
##################

loser_angry_pp <- brm(angryprop ~ post*populist_continuous + post*polarization
                      + incumbentparty + concurrent + runoff + semipresidential + enc_v1
                      + Xl + Xr 
                      + (1 + Xl + Xr | party_election),
                      data=losers,
                      warmup=1000, iter=2000, chains=3, seed=1234)
#plot(loser_angry_pp)
#summary(loser_angry_pp)

#save(winner_love_pp,
#     winner_angry_pp,
#     loser_love_pp, 
#     loser_angry_pp,
#     file="Stan_PopulistPartyContinuous.RData")




##################
# result summary #
##################

load("PSRM Replication Files/Stan_PopulistPartyContinuous.RData")

# plot interaction terms
getStdPostCI <- function(model, var1, var2, intr, label){
  coeftab <- summary(model)[[14]][,c(1,3,4)]
  coeftab <- coeftab[intr,]
  
  temp <- data.frame(label=label,
                     est=coeftab[1],
                     lwr=coeftab[2],
                     upr=coeftab[3])
  colnames(temp) <- c("label", "est", "lwr", "upr")
  return(temp)
}

citab <- rbind(getStdPostCI(winner_love_pp, 2, 3, 12, "Winner + Love"),
               getStdPostCI(winner_love_pp, 2, 4, 13, "Winner + Love"),
               getStdPostCI(winner_angry_pp, 2, 3, 12, "Winner + Angry"),
               getStdPostCI(winner_angry_pp, 2, 4, 13, "Winner + Angry"),
               getStdPostCI(loser_love_pp, 2, 3, 12, "Loser + Love"),
               getStdPostCI(loser_love_pp, 2, 4, 13, "Loser + Love"),
               getStdPostCI(loser_angry_pp, 2, 3, 12, "Loser + Angry"),
               getStdPostCI(loser_angry_pp, 2, 4, 13, "Loser + Angry"))

citab$intr <- rep(c("(a) Post Election × Populist Party Score", "(b) Post Election × Polarization"),
                  times=4)

citab$label <- factor(citab$label, levels=rev(unique(citab$label)))
citab$intr <- factor(citab$intr, levels=unique(citab$intr))

# figure N.5
ggplot(data = citab, 
       aes(x = est, y = label)) + 
  facet_wrap(. ~ intr, ncol = 2, scales = "free_x") +
  geom_errorbar(data = citab, aes(xmin = lwr, xmax = upr),
                lwd=0.8, width = 0.1) +
  geom_vline(xintercept=0, lwd=0.5, lty=2) +
  geom_point(cex=4, pch=19) +
  xlab("Posterior Mean and 95% Credible Interval") +
  ylab("") +
  theme_bw() +
  theme(axis.title.x = element_text(size = 15),
        axis.title.y = element_text(size = 15),
        axis.text=element_text(size=12),
        panel.spacing = unit(1, "lines"))

#ggsave("interaction_populistpartycontinuous.pdf", width=12, height=6)
